package com.orangelife.mobile.lookRepair.activity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;

public class PlaceOrderDetailActivity extends OrangeLifeBaseActivity {
	
	/**标题中间*/
	private TextView tvTitle;
	/** 标题右边*/
	private TextView tvTitleRight;
	/**缴费金额*/
	private TextView tvPay;
	/** 订单总额*/
	private TextView tvOrderTotal;
	/** 抵用券*/
	private TextView tvVoucher;
	/**抵用券*/
	private LinearLayout llVoucher;
	/**抵用券上面的线*/
	private View view;
	/**接收从后台传来的数据*/
	private Map<String,Object> mapDetail;
	private Dialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_look_repair_place_order_detail);
		initeView();
		findView();
	}
	
	/**
	 * 初始化
	 */
	private void initeView(){
		tvTitle = (TextView)findViewById(R.id.tv_title);
		tvTitleRight = (TextView)findViewById(R.id.tv_title_right);
		tvPay = (TextView)findViewById(R.id.tvPay);
		tvOrderTotal = (TextView)findViewById(R.id.tvOrderTotal);
		tvVoucher = (TextView)findViewById(R.id.tvVoucher);
		llVoucher = (LinearLayout)findViewById(R.id.llVoucher);
		view = (View)findViewById(R.id.view);
	}
	
	/**
	 * 赋值
	 */
	private void findView(){
		tvTitle.setText(R.string.order_detail);
		tvTitleRight.setVisibility(View.INVISIBLE);
		dialog = DialogHelper.getInstance().createLoadingDialog(
				PlaceOrderDetailActivity.this, Constant.LOADING);
		dialog.show();
		getDetail();
	}
	
	/**
	 * 设置详情
	 */
	private void setDetail(){
		String couponValue = mapDetail.get("couponValue").toString();
		tvPay.setText("¥"+mapDetail.get("goodsCost").toString());
		tvOrderTotal.setText("¥"+mapDetail.get("totalCost").toString());
		if("0".equals(couponValue)){
			llVoucher.setVisibility(View.GONE);
			view.setVisibility(View.GONE);
		}else{
			tvVoucher.setText(mapDetail.get("couponValue").toString()+"元抵用券");
		}
	}
	
	/**
	 * 获取详情
	 */
	
	private void getDetail(){
		Intent intent = getIntent();
		String repairCode = intent.getStringExtra("repairCode");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("{orderID}", repairCode);
		JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.URL_PLACE_ORDER_DETAIL, map, handler, 1, HttpUtil.ACTION_URI);
	}
	
	/**
	 * 接收处理线程返回的消息
	 */
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.WHAT_ERROR_HTTP:
				ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
				break;
			case 1:
				mapDetail = new HashMap<String, Object>();

				mapDetail = (Map<String, Object>) JSONHelper.jsonToMap(
						msg.obj.toString()).get("entity");
				DialogHelper.closeDialog(dialog);
				setDetail();
				break;
			
			}
			if (dialog != null) {
				dialog.dismiss();
			}
		};
	};

}
